﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin
{
    public partial class Form : System.Web.UI.MasterPage
    {
        FormPage _FormPage = null;
        public EnumHelper.FormState formState = EnumHelper.FormState.add;
        public int id = 0;
        public string idStr = "";
        public bool IsSuccess = true;
        public string ErrorString = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            _FormPage = Page as FormPage;
            if (id == 0) DataTypePlus.getPageRequest(Request.QueryString["id"], ref id);
            DataTypePlus.getPageRequest(Request.QueryString["idStr"], ref idStr);
            Security.chkIsLogined();
            if (id > 0 || idStr!="")
            {
                formState = EnumHelper.FormState.edit;
                _FormPage.Page_Load_DefaultModel();
            }
            _FormPage.Page_Load_More();
            if (!IsPostBack)
            {
                _FormPage.Form_Default();
                if (formState == EnumHelper.FormState.edit) _FormPage.Form_Bind();
            }
        }
        protected void bt_submit_Click(object sender, EventArgs e) { _FormPage.Form_Submit(); }
        /// <summary>
        /// 提交成功
        /// 默认文本
        /// </summary>
        public void SubmitOK() { SubmitOK(""); }
        /// <summary>
        /// 提交成功
        /// 定制文本
        /// </summary>
        /// <param name="okStr"></param>
        public void SubmitOK(string okStr)
        {
            if (okStr == "") okStr = EnumHelper.GetEnumDescription(((EnumHelper.FormState)Enum.Parse(typeof(EnumHelper.FormState), ((int)formState).ToString()))) + "成功";
            string jsStr = ScriptManager_Alert_float_Str(okStr,true);
            jsStr += ScriptManager_frame_list_page_build_Str(DataTypePlus.requestString(id));
            jsStr += ScriptManager_close_adminRight_Str();
            ScriptManager_js(jsStr);

            //ScriptManager_Alert_float(okStr, true);
            //ScriptManager_frame_list_page_build(DataTypePlus.requestString(id));
            //ScriptManager_close_adminRight();
        }
        /// <summary>
        /// 判断是否不重复
        /// </summary>
        /// <param name="sqlChongfuChk">要判断的字段SQL语句</param>
        /// <param name="returnMsg">返回错误信息</param>
        /// <returns>不重复=true</returns>
        public bool noChongfu(string sqlChongfuChk, string returnMsg)
        {
            if (formState == EnumHelper.FormState.edit) sqlChongfuChk += " and  id<>'" + id + "'";
            if (_FormPage.GetCount(buildSql(sqlChongfuChk)) > 0)
            {
                ScriptManager_Alert_float(returnMsg, false);
                return false;
            }
            else return true;
        }
        /// <summary>
        /// 判断非空值是否不重复
        /// </summary>
        /// <param name="val">要判断的值</param>
        /// <param name="sqlChongfuChk">要判断的值所在字段SQL语句</param>
        /// <param name="returnMsg">返回错误信息</param>
        /// <returns>空串||不重复=true</returns>
        public bool noChongfu(string val, string sqlChongfuChk, string returnMsg)
        {
            if (val != "") return noChongfu(sqlChongfuChk, returnMsg);
            else return true;
        }
        public bool noErr(bool err, string returnMsg)
        {
            if (err)
            {
                ScriptManager_Alert_float(returnMsg, false);
                return false;
            }
            else return true;
        }
        public bool noZero(int v_, string returnMsg)
        {
            if (v_<=0)
            {
                ScriptManager_Alert_float(returnMsg, false);
                return false;
            }
            else return true;
        }
        public bool noZero(decimal v_, string returnMsg)
        {
            if (v_ <= 0)
            {
                ScriptManager_Alert_float(returnMsg, false);
                return false;
            }
            else return true;
        }
        public  bool noEmpty(string val, string returnMsg)
        {
            if (val != "") return true;
            else
            {
                ScriptManager_Alert_float(returnMsg, false);
                return false;
            }
        }
        public void HideSubmit() { bt_submit.Style.Add("display", "none"); }
        public void ShowSubmit() { bt_submit.Style.Add("display", "block"); }
        string buildSql(string sql_)
        {
            sql_ = sql_.ToLower().Trim();
            if (sql_.ToLower().Replace("and", "").Replace(" ", "") == "") return "";
            else
            {
                if (sql_.Substring(0, 3) == "and") return sql_;
                else return " and " + sql_;
            }
        }
        //
        public void ScriptManager_js(string jsStr)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", jsStr, true);
        }
        //
        public  void ScriptManager_Alert_float(string msgtitle, bool isSuccess)
        {
            ScriptManager_js(ScriptManager_Alert_float_Str(msgtitle, isSuccess));
        }
        public string ScriptManager_Alert_float_Str(string msgtitle, bool isSuccess)
        {
            string msgcss = "correct";
            if (!isSuccess) msgcss = "error";
            return "parent.Alert_float_(\"" + msgtitle + "\",\"" + msgcss + "\"); ";
        }
        public  void ScriptManager_frame_list_page_build(string fenghuolun_TrDatakey)
        {
            ScriptManager_js( ScriptManager_frame_list_page_build_Str(fenghuolun_TrDatakey));
        }
        public string ScriptManager_frame_list_page_build_Str(string fenghuolun_TrDatakey)
        {
            return "parent.frame_list_page_build(\"" + fenghuolun_TrDatakey + "\");";
        }
        public  void ScriptManager_close_adminRight()
        {
            ScriptManager_js(ScriptManager_close_adminRight_Str());
        }
        public string ScriptManager_close_adminRight_Str()
        {
            return "parent.close_adminRight();";
        }
    }
}